package com.vrv.common.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.vrv.common.WaitingDialog;
import com.vrv.common.resolver.Page;
import com.vrv.common.resolver.RequestParameter;
import com.vrv.common.resolver.Response;
import com.vrv.common.resolver.ResultStatus;
import com.vrv.common.resolver.remote.RequestUtils;
import com.vrv.common.util.ViewUtils;
import com.vrv.netmgr.EdpApplication;

public abstract class CommonFormActivity extends Activity {

    /** 标题[子类定制] */
    protected String caption = "未知标题";
    /** 详情页面需要展现的字段KEY[子类定制] */
    protected String[] detailKeys = {};
    /** 列表页面需要展现的字段KEY[子类定制] */
    protected String[] listKeys = {};
    /** 请求参数[子类定制] */
    protected RequestParameter parameter = null;
    /** 等待对话框 */
    private WaitingDialog progressDialog;

    private View actionView = null;

    protected TextView tv_title = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView(savedInstanceState);
        actionView = getActionView();
        prepareData(savedInstanceState);

        actionView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                parameter = getRequestParameterInstance();
                EdpApplication.parameter = parameter;
                requestServer();
            }
        });
    }

    /**
     * 必须返回一个用于触发事件跳转到list页面的视图对象
     *
     * @return
     */
    protected abstract View getActionView();

    /**
     * 返回一个可用于请求服务的RequestParameter对象
     *
     * @return
     */
    protected abstract RequestParameter getRequestParameterInstance();

    protected abstract void initView(Bundle savedInstanceState);

    protected abstract void prepareData(Bundle savedInstanceState);

    /**
     * 请求服务器
     */
    private void requestServer() {
        // progressDialog = ProgressDialog.show(CommonFormActivity.this, "",
        // "查询中...", true, true);
        progressDialog = WaitingDialog.createDialog(getApplicationContext());
        new Thread(new Runnable() {
            @Override
            public void run() {
                // Response response = RequestUtils.getInstance().request(parameter, detailKeys);
                String resultString = RequestUtils.getInstance().request(parameter, detailKeys);
                Response response = RequestUtils.getInstance().parse(resultString, detailKeys);
                if (response == null) {
                    handler.sendEmptyMessage(ResultStatus.netError.getCode());
                } else if (!response.isSuccess()) {
                    handler.sendEmptyMessage(ResultStatus.bizError.getCode());
                } else if (response.isSuccess() && response.getTotalRecords() > 0 && response.getRecords() != null && response.getRecords().size() > 0) {
                    EdpApplication.page = new Page(response);
                    handler.sendEmptyMessage(ResultStatus.success.getCode());
                } else {
                    handler.sendEmptyMessage(ResultStatus.noneResult.getCode());
                }
            }
        }).start();
    }

    /**
     * 处理数据
     */
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                ViewUtils.dismissProgressDialog(getApplicationContext(), progressDialog, ResultStatus.netError.getMsg());
                break;
            case 1:
                ViewUtils.dismissProgressDialog(getApplicationContext(), progressDialog, ResultStatus.bizError.getMsg());
                break;
            case 2:
                ViewUtils.dismissProgressDialog(getApplicationContext(), progressDialog, ResultStatus.noneResult.getMsg());
                break;
            case 3:

                Intent intent = new Intent(getApplicationContext(), CommonListActivity.class);
                intent.putExtra("detailKeys", detailKeys);
                intent.putExtra("listKeys", listKeys);
                intent.putExtra("caption", caption);
                startActivity(intent);

                ViewUtils.dismissProgressDialog(getApplicationContext(), progressDialog, ResultStatus.success.getMsg());
                break;
            default:
                break;
            }
        }
    };

}
